package com.sdp.irrigation.controller;


import java.time.LocalDateTime;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sdp.core.bizc.db.Query;
import com.sdp.core.bizc.utils.UniqueCheckTools;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.irrigation.db.irrigation_eq.service.IIrrigationEqService;
import com.sdp.irrigation.db.irrigation_eq_real_time.service.IIrrigationEqRealTimeService;
import com.sdp.irrigation.db.irrigation_pump_well.service.IIrrigationPumpWellService;
import com.sdp.irrigation.entity.IrrigationEq;
import com.sdp.irrigation.entity.IrrigationEqRealTime;
import com.sdp.irrigation.entity.IrrigationPumpWell;
import com.sdp.irrigation.utils.IrrigationConstants;

/**
 * <p>
 * 设备台账 前端控制器
 * </p>
 *
 * @author zsp
 * @since 2024-12-14
 */
@RestController
@RequestMapping("/IrrigationEqController")
public class IrrigationEqController {
	
	@Autowired
	private IIrrigationEqService iIrrigationEqService;

	@Autowired
	private Query query;
	
	@Autowired
	private UniqueCheckTools uniqueCheckTools;
	
	@Autowired
	private IIrrigationPumpWellService iIrrigationPumpWellService;

	@Autowired 
	private IIrrigationEqRealTimeService eqRealTimeService;
	
	@PostMapping(value = "/page")
	public Result<?> page(@RequestBody DataCenter dataCenter) {
		return Result.ok(query.page(iIrrigationEqService, IrrigationEq.class, dataCenter));
	}
	
	@PostMapping(value = "/save")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> save(@RequestBody IrrigationEq eq) {
		if(eq==null) {
			return Result.error("设备信息为空");
		}
		if(StringUtils.isEmpty(eq.getKeyId())) {
			Result<?> r = uniqueCheckTools.isUniqueSingle(iIrrigationEqService, IrrigationEq.class, new String[] {"eqNo"}, "keyId", eq, null, false);
			if(r==null || !r.isSuccess()) {
				return Result.error("设备编号不能重复");
			}
			eq.setCreateTime(LocalDateTime.now());
			eq.setUpdateTime(LocalDateTime.now());
			eq.setEqUseState(IrrigationConstants.设备使用状态_空闲.getCode());
			eq.setEqRepairStatus(IrrigationConstants.设备报修状态_未报修.getCode());
			iIrrigationEqService.save(eq);
			
			//新增时添加设备实时状态信息
			IrrigationEqRealTime eqRealTime = new IrrigationEqRealTime();
			eqRealTime.setEqNo(eq.getEqNo());
			eqRealTimeService.save(eqRealTime);
		}else {
			Result<?> r = uniqueCheckTools.isUniqueSingle(iIrrigationEqService, IrrigationEq.class, new String[] {"eqNo"}, "keyId", null, eq, false);
			if(r==null || !r.isSuccess()) {
				return Result.error("设备编号不能重复");
			}
			eq.setUpdateTime(LocalDateTime.now());
			iIrrigationEqService.updateById(eq);
		}
		
		//将设备的封面图更新到水井信息中
		iIrrigationPumpWellService.updateCoverimg(eq.getEqCoverimg(), eq.getEqNo());
		
		return Result.ok();
	}
	
	/**
	 * 设置坐标
	 * @param eq
	 * @return
	 */
	@PostMapping(value = "/setCoordinate")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> setCoordinate(@RequestBody IrrigationEq eq) {
		if(eq==null || StringUtils.isEmpty(eq.getKeyId())) {
			return Result.error("设备信息为空");
		}
		IrrigationEq dbEq = iIrrigationEqService.getById(eq.getKeyId());
		if(StringUtils.isEmpty(dbEq)) {
			return Result.error("选择设备不存在");
		}
		if(!dbEq.getEqNo().equals(eq.getEqNo())) {
			return Result.error("不能修改已存在设备的编号");
		}
		iIrrigationEqService.updateById(eq);
		return Result.ok(eq);
	}
	
	@GetMapping(value="/queryEqByEqNo")
	public Result<?> queryEqByEqNo(@RequestParam(name="eqNo",required=true) String eqNo) {
		IrrigationEq eq = iIrrigationEqService.queryEqByEqNo(eqNo);
		if(StringUtils.isEmpty(eq)) {
			return Result.error("此设备不存");
		}
		
		//根据设备编号，查询当前设备所属机井
		IrrigationPumpWell pumpWell = iIrrigationPumpWellService.queryPumpWellByEqNo(eqNo);
		if(!StringUtils.isEmpty(pumpWell)) {
			eq.setPwNo(pumpWell.getPwNo());
		}
		return Result.ok(eq);
	}
	
}

